// pages/mine/mine.js
import {getUserInfo, makePhoneCall} from '../../utils/util'
import {logout} from '../../utils/apis'

const app = getApp();
Page({
    data: {
        username: '未注册',
        phone: '',
        birthday: '',
        clearModalVisible: false,
        clearActions: [
            {
                name: '取消'
            },
            {
                name: '删除',
                color: '#ed3f14',
                loading: false
            }
        ]
    },
    onLoad: function (options) {
        var {loginInfo}=app.globalData;
        if (!loginInfo||JSON.stringify(loginInfo)==='{}') {
            return;
        }
        this.setData({
            'username':loginInfo.username,
            'phone':loginInfo.mobile,
            'birthday':loginInfo.birthday
        });
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
        var that = this
        app.getLoginInfo(loginInfo => {
            that.setData({
                loginInfo: loginInfo.user_info
            })
        })
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    onPhoneTap(e) {
        makePhoneCall(e.currentTarget.dataset.phone)
    },
    onLogout(e) {
        var that = this
        var {loginInfo: {phone}, loading} = this.data
        if (loading) {
            return
        }
        this.setData({
            loading: true
        })
        logout({
            phone,
            success(data) {
                app.setLoginInfo(data)
                that.setData({
                    loginInfo: null,
                    loading: false
                })
            }
        })
    },
    callback(loginInfo) {
        this.setData({
            loginInfo: loginInfo.user_info
        })
    },
    onShareAppMessage() {
        return {
            title: '我的信息',
            path: '/pages/mine/mine'
        }
    },
    handleOpenClear() {
        console.log("aa");
        this.setData({
            clearModalVisible: true
        });
    },
    handleClearStore({detail}) {
        if (detail.index === 0) {
            this.setData({
                clearModalVisible: false
            });
        } else {
            const action = [...this.data.clearActions];
            action[1].loading = true;

            this.setData({
                clearActions: action
            });

            setTimeout(() => {
                wx.clearStorageSync();
                action[1].loading = false;
                this.setData({
                    clearModalVisible: false,
                    clearActions: action
                });
            }, 1000);
        }
    }
})